/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package reflect;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Administrator
 */
public class LoggingHandler implements InvocationHandler{
    
    private Object target;
    
    public Object bind(Object target){
        this.target = target;
        return Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), this);
        
    }
    
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    
        Object result = null;
        
        try {
            log(String.format("%s() 呼叫开始...", method.getName()));
            result = method.invoke(target, args);
            log(String.format("%s() 呼叫结束...", method.getName()));
        } catch (IllegalAccessException | IllegalArgumentException e) {
            log(e.toString());
        }
        
        return result;
    }
    
    private void log(String message){
    
        Logger.getLogger(LoggingHandler.class.getName()).log(Level.INFO, message);
    }
}
